﻿using Newtonsoft.Json; 
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Dto.Center.ProductBase;
using SHKF.B2C.Model.Dto.Center.ProductBaseImage;
using SHKF.B2C.Model.Dto.WorkingCenter;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Entity.WorkingCenter;
using SHKF.B2C.Model.Req;
using SHKF.B2C.Network;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; 

namespace SHKF.B2C.Mannage.AdminUI.Network
{
    public class HttpClientManage : HttpClientBase
    { 
        private string urlQueryCompanyList = "/company/list";
        private string urlCompanySave = "/company/save";
        private string urlCompanyDelete = "/company/delete";

        private string urlShopGet = "/shop/get";
        private string urlShopList = "/shop/list";
        private string urlShopSave = "/shop/save";
        private string urlShopDelete = "/shop/delete";
   
        private string urlShopMapList = "/shop/Map/list";
        private string urlShopMapSaveBatch = "/shop/Map/save/batch";
        private string urlShopMapSave = "/shop/Map/save";
        private string urlShopMapDelete = "/shop/Map/delete";

        private string urlUserList = "/user/list";
        private string urlUserSave = "/user/save";
        private string urlUserDelete = "/user/delete";

        //商品库
        private string urlProductBaseQuery = "/ProductBase/list";
        private string urlProductBaseSave = "/ProductBase/Save";
        private string urlProductBaseDelete = "/ProductBase/Delete";

        //商品库图片
        private string urlProductBaseImageQuery = "/ProductBase/Image/list";
        private string urlProductBaseImageAdd = "/ProductBase/Image/Add";
        private string urlProductBaseImageDelete = "/ProductBase/Image/Delete";

        private string urlProductQuery = "/product/list";
        private string urlBatchDeleteProduct = "/purlProductSaveAndMaproduct/batch/delete";
        private string urlProductMapList = "/product/map/list";
        private string urlProductMapSave = "/product/map/save"; 
        private string urlProductMapDelete = "/product/map/delete";

        private string urlProductErpList = "/product/erp/list";  //查询erp产品信息　


        //分仓
        private string urlQueryDataWarehousInfoList = "/DataWarehousInfo/list";
        private string urlSaveDataWarehousInfo = "/DataWarehousInfo/save";
        private string urlDeleteDataWarehousInfo = "/DataWarehousInfo/delete";

        //erp分店映射
        private string urlShopRelatedList = "/ShopRelated/list";
        private string urlShopRelatedSave = "/ShopRelated/save";
        private string urlShopRelatedDelete = "/ShopRelated/delete";
        private string urlShopRelatedBatchUpdate = "/ShopRelated/BatchUpdate";

        //erp分店产品映射
        private string urlProductRelatedList = "/ProductRelated/list";
        private string urlProductRelatedSave = "/ProductRelated/save";
        private string urlProductRelatedDelete = "/ProductRelated/delete";

        /// <summary>
        /// erp数据库实例
        /// </summary>
        private string urlErpServerList = "/ErpServer/list";
        private string urlErpServerSave = "/ErpServer/save";
        private string urlErpDataBaseList = "/ErpDataBase/list";
        private string urlErpDataBaseSave = "/ErpDataBase/save";

        public HttpClientManage()
        {
            string uriString = GL.uriString;
            base.BaseAddress = new Uri(uriString);
        }

        #region 公司

        public async Task<CompanyListDto> QueryCompanyList(string CompanyName)
        {
            string url = base.BaseAddress.OriginalString + this.urlQueryCompanyList + "?";
            url += $"CompanyName=" + CompanyName.ToString();

            CompanyListDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<CompanyListDto>();
            }
            else
            {
                dto = new CompanyListDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }

        public async Task<CompanyDto> CompanySave(CompanyEntity company)
        { 
            CompanyDto dto = await base.PostAsync<CompanyDto>(this.urlCompanySave, company);
 
            return dto;
        }

        public async Task<DataDto> CompanyDelete(long SN)
        {
            DataDto dto = new DataDto();
            
            string requestUri = urlCompanyDelete;
            Dictionary<string, object> parsmList = new Dictionary<string, object>();
            parsmList.Add("SN", SN);
            dto = await base.GetAsync<DataDto>(requestUri, parsmList);
 
            return dto;
        }
         
        #endregion


        #region 门店

        public async Task<ShopMapsDto> ShopMapList(long ShopSN)
        {
            string url = base.BaseAddress.OriginalString + this.urlShopMapList + "?";
            url += $"ShopSN={ShopSN}";

            ShopMapsDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<ShopMapsDto>();
            }
            else
            {
                dto = new ShopMapsDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }

        /// <summary>
        /// 保存门店映射
        /// </summary>
        /// <param name="shopCEntity"></param>
        /// <returns></returns>
        public async Task<DataDto> ShopMapSave(ShopMapEntity shopCEntity)
        {
            DataDto dto = new DataDto();
            string json = JsonConvert.SerializeObject(shopCEntity);
            StringContent stringContent = new StringContent(json);
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            string url = base.BaseAddress.OriginalString + urlShopMapSave;
            HttpResponseMessage httpResponseMessage = await base.PostAsync(url, stringContent);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<DataDto>();
            }
            else
            {
                dto = new DataDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }

        public async Task<DataDto> ShopMapSaveBatch(List<ShopMapEntity> shopMapList)
        { 
            DataDto dto = await base.PostAsync<DataDto>(urlShopMapSaveBatch, shopMapList);
            return dto;
        }


        public async Task<DataDto> ShopMapDelete(long SN)
        {
            string url = base.BaseAddress.OriginalString + this.urlShopMapDelete + "?";
            url += $"SN={SN}";

            DataDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<DataDto>();
            }
            else
            {
                dto = new DataDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }

        public async Task<ShopDto> ShopGet(long SN)
        {
            string url = base.BaseAddress.OriginalString + this.urlShopGet + "?";
            url += $"SN={SN}";

            ShopDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<ShopDto>();
            }
            else
            {
                dto = new ShopDto();
                dto.SetError(httpResponseMessage);
            }

            return dto;
        }

        public async Task<ShopListDto> ShopQuery(int CompanySN, string ShopName)
        {
            string url = base.BaseAddress.OriginalString + this.urlShopList + "?";
            url += $"ShopName={ShopName}";
            url += $"&CompanySN={CompanySN}";

            ShopListDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<ShopListDto>();
            }
            else
            {
                dto = new ShopListDto();
                dto.SetError(httpResponseMessage);
            }

            return dto;
        }

        public async Task<ShopDto> ShopSave(ShopEntity shopEntity)
        {
            ShopDto dto = new ShopDto();
            string json = JsonConvert.SerializeObject(shopEntity);
            StringContent stringContent = new StringContent(json);
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            string url = base.BaseAddress.OriginalString + urlShopSave;
            HttpResponseMessage httpResponseMessage = await base.PostAsync(url, stringContent);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<ShopDto>();
            }
            else
            {
                dto = new ShopDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }

        public async Task<DataDto> ShopDelete(long SN)
        {
            string url = base.BaseAddress.OriginalString + this.urlShopDelete + "?";
            url += $"SN={SN}";

            DataDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<DataDto>();
            }
            else
            {
                dto = new DataDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }


        /// <summary>
        /// 获取有赞运总部ID
        /// </summary>
        /// <param name="CompanySN"></param>
        /// <returns></returns>
        public async Task<DataDto> ShopGetKdtId(long CompanySN)
        {
            string url = base.BaseAddress.OriginalString + "/shop/yzy.kdt.id?";
            url += $"CompanySN={CompanySN}";

            //DataDto dto = null;
            //HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            //if (httpResponseMessage.IsSuccessStatusCode)
            //{
            //    dto = await httpResponseMessage.Content.ReadAsAsync<DataDto>();
            //}
            //else
            //{
            //    dto = new DataDto();
            //    dto.SetError(httpResponseMessage);
            //}

            DataDto dto = await base.GetAsync<DataDto>(url, null); 
            return dto;
        }


        #endregion

        #region 分仓基础信息

        /// <summary>
        /// 数据库实例列表
        /// </summary>
        /// <returns></returns>
        public async Task<ErpServerListDto> QueryErpServer()
        {
            string url = base.BaseAddress.OriginalString + this.urlErpServerList + "?"; 
            ErpServerListDto dto = await base.GetAsync<ErpServerListDto>(url);
            return dto;
        }

        public async Task<DataDto> SaveErpServer(ErpServerEntity data)
        {
            string url = base.BaseAddress.OriginalString + this.urlErpServerSave;
            DataDto dto = await base.PostAsync<DataDto>(url, data);
            return dto;
        }

        public async Task<ErpDataBaseListDto> QueryErpDataBase(int fksn)
        {
            string url = base.BaseAddress.OriginalString + this.urlErpDataBaseList + "?fksn=" + fksn.ToString();
            ErpDataBaseListDto dto = await base.GetAsync<ErpDataBaseListDto>(url);
            return dto;
        }

        public async Task<DataDto> SaveErpDataBase(ErpDataBaseEntity data)
        {
            string url = base.BaseAddress.OriginalString + this.urlErpDataBaseSave;
            DataDto dto = await base.PostAsync<DataDto>(url, data);
            return dto;
        }

        /// <summary>
        /// 分仓基础信息列表
        /// </summary>
        /// <param name="ShopVSN"></param>
        /// <returns></returns>
        public async Task<DataWarehousInfoListDto> QueryDataWarehousInfoList(string keyword)
        {
            string url = base.BaseAddress.OriginalString + this.urlQueryDataWarehousInfoList + "?";
            url += $"keyword=" + keyword;

            DataWarehousInfoListDto dto = await base.GetAsync<DataWarehousInfoListDto>(url);
            return dto;
        }

        /// <summary>
        /// 分仓基础信息保存
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<DataWarehousInfoDto> SaveDataWarehousInfo(DataWarehousInfoEntity info)
        {
            DataWarehousInfoDto dto = await base.PostAsync<DataWarehousInfoDto>(this.urlSaveDataWarehousInfo, info);
            return dto;
        }

        /// <summary>
        /// 分仓基础信息删除
        /// </summary>
        /// <param name="SN"></param>
        /// <returns></returns>
        public async Task<DataDto> DeleteDataWarehousInfo(long SN)
        {
            DataDto dto = new DataDto();

            string requestUri = urlDeleteDataWarehousInfo;
            Dictionary<string, object> parsmList = new Dictionary<string, object>();
            parsmList.Add("SN", SN);
            dto = await base.GetAsync<DataDto>(requestUri, parsmList);
            return dto;
        }
         
        #endregion

        #region 分仓映射

        /// <summary>
        /// 分仓映射列表
        /// </summary>
        /// <param name="ShopVSN"></param>
        /// <returns></returns>
        public async Task<ShopRelatedListDto> QueryShopRelatedList(int ShopVSN)
        {
            string url = base.BaseAddress.OriginalString + this.urlShopRelatedList + "?";
            url += $"ShopVSN=" + ShopVSN.ToString();

            ShopRelatedListDto dto = await base.GetAsync<ShopRelatedListDto>(url); 
            return dto;
        }

        /// <summary>
        /// 分仓映射保存
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<ShopRelatedDto> SaveShopRelated(ShopRelatedEntity info)
        {
            ShopRelatedDto dto = await base.PostAsync<ShopRelatedDto>(this.urlShopRelatedSave, info); 
            return dto;
        }

        /// <summary>
        /// 分仓映射批量保存
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<ShopRelatedDto> SaveBatchShopRelated(List<ShopRelatedEntity> info)
        {
            ShopRelatedDto dto = await base.PostAsync<ShopRelatedDto>(this.urlShopRelatedBatchUpdate, info);
            return dto;
        }


        /// <summary>
        /// 分仓映射删除
        /// </summary>
        /// <param name="SN"></param>
        /// <returns></returns>
        public async Task<DataDto> DeleteShopRelated(long SN)
        {
            DataDto dto = new DataDto();

            string requestUri = urlShopRelatedDelete;
            Dictionary<string, object> parsmList = new Dictionary<string, object>();
            parsmList.Add("SN", SN);
            dto = await base.GetAsync<DataDto>(requestUri, parsmList); 
            return dto;
        }

        #endregion

        #region 分仓产品映射

        /// <summary>
        /// 分仓产品映射列表
        /// </summary>
        /// <param name="ProductVSN"></param>
        /// <returns></returns>
        public async Task<ProductInfoRelatedListDto> QueryProductRelatedList(long ProductVSN)
        {
            string url = base.BaseAddress.OriginalString + this.urlProductRelatedList + "?";
            url += $"ProductVSN=" + ProductVSN.ToString();

            ProductInfoRelatedListDto dto = await base.GetAsync<ProductInfoRelatedListDto>(url);
            return dto;
        }

        /// <summary>
        /// 分仓产品映射保存
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task<ProductInfoRelatedDto> SaveProductInfoRelated(ProductInfoRelatedEntity info)
        {
            ProductInfoRelatedDto dto = await base.PostAsync<ProductInfoRelatedDto>(this.urlProductRelatedSave, info);
            return dto;
        }

        /// <summary>
        /// 分仓产品映射删除
        /// </summary>
        /// <param name="SN"></param>
        /// <returns></returns>
        public async Task<DataDto> DeleteProductRelated(long SN)
        {
            DataDto dto = new DataDto();

            string requestUri = this.urlProductRelatedDelete;
            Dictionary<string, object> parsmList = new Dictionary<string, object>();
            parsmList.Add("SN", SN);
            dto = await base.GetAsync<DataDto>(requestUri, parsmList);
            return dto;
        }


        #endregion

        #region 用户

        public async Task<DataDto<List<UserEntity>>> QueryUserList(int CompanySN, int ShopSN, string UserName)
        {
            Dictionary<string, object> paraList = new Dictionary<string, object>();
            paraList.Add("CompanySN", CompanySN);
            paraList.Add("ShopSN", ShopSN); 
            paraList.Add("UserName", UserName);
            DataDto<List<UserEntity>> dto = await base.GetAsync<DataDto<List<UserEntity>>>(urlUserList, paraList);

            return dto;
        }

        public async Task<DataDto<UserEntity>> SaveUser(UserEntity user)
        {
            DataDto<UserEntity> dto = await base.PostAsync<DataDto<UserEntity>>(urlUserSave, user);
            return dto;
        }

        public async Task<DataDto<int>> DeleteUser(long SN)
        {
            Dictionary<string, object> paraList = new Dictionary<string, object>();
            paraList.Add("SN", SN);
            DataDto<int> dto = await base.GetAsync<DataDto<int>>(urlUserDelete, paraList);

            return dto;
        }

        #endregion

        #region 商品库 


        /// <summary>
        /// 查询商品
        /// </summary>
        /// <param name="Upc"></param>
        /// <param name="ProductName"></param>
        /// <param name="LiceseId"></param>
        /// <param name="CommonTitle"></param>
        /// <returns></returns>
        public async Task<ProductBasePageDto> ProductBaseQuery(ProductBasePageReq req)
        {
            ProductBasePageDto dto = await this.PostAsync<ProductBasePageDto>(urlProductBaseQuery, req);
            return dto;
        }

        /// <summary>
        /// 商品更新
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<ProductBaseDto> ProductBaseSave(ProductBaseEntity data)
        { 
            ProductBaseDto dto = await this.PostAsync<ProductBaseDto>(urlProductBaseSave, data);
            return dto;
        }

        /// <summary>
        /// 商品删除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<DataDto> ProductBaseDelete(List<long> data)
        {
            DataDto dto = await this.PostAsync<DataDto>(urlProductBaseDelete, data);
            return dto;
        }

        #endregion


        #region 产品

        #region 商品库图片

        public async Task<ProductBaseImageListDto> ProductBaseImageQuery(long FKSN)
        {
            Dictionary<string, object> paramsList = new Dictionary<string, object>();
            paramsList.Add("FKSN", FKSN); 
            ProductBaseImageListDto dto = await this.GetAsync<ProductBaseImageListDto>(urlProductBaseImageQuery, paramsList);
            return dto;
        }

        /// <summary>
        /// 商品图片添加
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>

        public async Task<ProductBaseImageDto> ProductBaseImageAdd(ProductBaseImageEntity data)
        {
            ProductBaseImageDto dto = await this.PostAsync<ProductBaseImageDto>(urlProductBaseImageAdd, data);
            return dto; 
        }

        /// <summary>
        /// 商品图片删除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<DataDto> ProductBaseImageDelete(ProductBaseImageEntity data)
        {
            DataDto dto = await this.PostAsync<DataDto>(urlProductBaseImageDelete, data);
            return dto;
        }


        #endregion



        public async Task<ProductListDto> QueryProduct(long CompnaySN, long ShopSN, string ProductName)
        {
            string url = this.urlProductQuery + "?"; 
            url += $"&ProductName={ProductName}";
            url += $"&CompnaySN={CompnaySN}";
            url += $"&ShopSN={ShopSN}";

            ProductListDto dto = null;
            HttpResponseMessage httpResponseMessage = await base.GetAsync(url);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<ProductListDto>();
            }
            else
            {
                dto = new ProductListDto();
                dto.SetError(httpResponseMessage);
            }

            return dto;
        }

        /// <summary>
        /// 批量删除产品
        /// </summary>
        /// <returns></returns>
        public async Task<DataDto> BatchDeleteProduct(List<ProductInfoEntity> products)
        {
            DataDto dto = new DataDto();
            string json = JsonConvert.SerializeObject(products);
            StringContent stringContent = new StringContent(json);
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            string url = GL.uriString + urlBatchDeleteProduct;
            HttpResponseMessage httpResponseMessage = await base.PostAsync(url, stringContent);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                dto = await httpResponseMessage.Content.ReadAsAsync<DataDto>();
            }
            else
            {
                dto = new DataDto();
                dto.SetError(httpResponseMessage);
            }
            return dto;
        }

        /// <summary>
        /// 店铺产品SN
        /// </summary>
        /// <param name="FKSN"></param>
        /// <returns></returns>
        public async Task<ProductInfoMapListDto> ProductMapQuery(long FKSN)
        {
            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("FKSN", FKSN);

            ProductInfoMapListDto dto = await base.GetAsync<ProductInfoMapListDto>(urlProductMapList, para); 
            return dto;
        }

        public async Task<ProductInfoMapListDto> ProductMapSave(ProductInfoMapEntity map)
        { 
            ProductInfoMapListDto dto = await base.PostAsync<ProductInfoMapListDto>(urlProductMapSave, map);
            return dto;
        }

        public async Task<ProductInfoMapListDto> ProductMapDelete(long SN)
        {
            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("SN", SN);
            ProductInfoMapListDto dto = await base.GetAsync<ProductInfoMapListDto>(urlProductMapDelete, para);
            return dto;
        }

        public async Task<ErpProductInfoListDto> QueryProductErpList(string ServerName, string WarehousSN, string KeyWord)
        {
            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("ServerName", ServerName);
            para.Add("KeyWord", KeyWord);
            para.Add("warehousSN", WarehousSN);

            ErpProductInfoListDto dto = await base.GetAsync< ErpProductInfoListDto>(this.urlProductErpList, para); 
            return dto;
        }


        #endregion


    }
}
